$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	var role = $("#role").val();//获取当前登录用户的角色
	/***************************    权限管理列表页    ***************************/

	//开启,暂停
	$("#btn-enable,#btn-disable").click(function(){
		if($(this).attr("id") == "btn-enable"){
			url = ctx+"/advertiser/main/authority/enable";
		}else if($(this).attr("id") == "btn-disable"){
			url = ctx+"/advertiser/main/authority/disable";
		}
		singleOper($(this),url);
	});
	
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/advertiser/main/authority/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/advertiser/main/authority/disable";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑账号    ***************************/
	
	//获取广告主
	if(role == "agent"){
		//全部广告主
		$("#advertiser_son_ul").hide();
		if($("#advertiserId").val() == 0 || $("#advertiserId").val() == ""){
 			$("#advertiserul_span").text("请选择广告主");
 			if($("#main_advertiserul").html()){
				$("#advertiser_son_ul").append("<li value='0' style='width:130px;'>请选择广告主</li>");
				var url = ctx+"/widget/selector/provide/advertiser?parentId="+$("#sessionagentId").val();
				$.getJSON( url, function(data) {
					var data_1 = data.advertiserList;
					$.each( data_1, function( key, val ) {
						$("#advertiser_son_ul").append('<li value='+ val.value +'>' + val.label + '</li>');
						if($("#advertiserId").val() == val.value){
							$("#advertiserul_span").html(val.label);
						}
					});
				});
			}
 		}else{
 			$("#advertiserul_span").html($("#advName").val())
 		}
		
	}else if(role == "advertiser"){
		$("#advertiserul_span").text($("#sessionadvertiserName").val());
		$("#advertiserId").val($("#sessionadvertiserId").val());
		$("#advertiserName").val($("#sessionadvertiserName").val());
	}
	 $("#advertiserul_span").on("click",function(){
	 	if(role == "agent"){
	 		if($('#advertiser_son_ul').height()>210){
	 			$('#advertiser_son_ul').addClass("son_ul_change");
	 		}
	 		$('#advertiser_son_ul').show(); //ul显示
	 	}
	 });
	 $('ul#advertiser_son_ul li').live("click",function(){
	 	$(this).parents('li').find('span').text($(this).text());
		$("#advertiserId").val($(this).attr("value"));
		$("#advertiserName").val($(this).text());
		$('#advertiser_son_ul').hide(); //ul隐藏
	});
	$("#advertiserul_box").mouseleave(function(){
		$('#advertiser_son_ul').hide(); //ul隐藏
		$('#advertiser_son_ul').removeClass("son_ul_change");
	});
	
	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
 	
	$("#footBtn .btn-save").click(function(){
		var authority = "";
		$("#checkboxs input[type=checkbox]:checked").each(function(){
		    	authority+=$(this).val()+",";
		});
		authority = authority.substring(0,authority.length-1);
		$("#authority").val(authority);
		if($("#accountId").val() == "0"){//创建账号时校验账号是否重复
			var flag = isExistName();//判断账号名称是否重复
			if(flag){
				return false;
			}
		}
		if($("#ensurePassword").val() !="" && $("#ensurePassword").val() !=$("#password").val()){
			$("#ensurePassword").css("box-shadow","0 0 6px #ff0000").attr('title','确认密码必须与密码一致！');
			$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("确认密码必须与密码一致！");
			return false;
		}
		if (!$("#validateForm").validate()) {
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$("#footBtn .btn-cancel").click(function(){
		window.location.href = ctx+"/advertiser/main/authority/list";
	});
	
	//个人信息保存按钮
	$("#btn-save-account").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		var myform = $("#validateForm").serialize();
		$.post(ctx+"/advertiser/main/authority/saveloginperson",myform,function(data){
			if($("#password").val() == "" && $("#ensurePassword").val() == ""){
				window.location.href = ctx+"/advertiser/main/dashboard";
			}else{
				window.location.href = ctx+"/login";
			}
		});
		return false;
	});
	
	//个人信息取消按钮
	$("#btn-cancel-account").unbind('click').click(function(){
		location.href = ctx+"/advertiser/main/dashboard";
	});
	
	$("#accountName").blur(function(){
		if($(this).val() != ""){
			var reg=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if($(this).val().match(reg)!=null){
				$(this).css("box-shadow","none").attr('title','');
				$(this).parent("div").find("span.spanVaildate").html("");
				isExistName();
			}else{
				$(this).css("box-shadow","0 0 6px #ff0000").attr('title','格式必须为邮箱类型， 如: example@email.com');
				$(this).parent("div").find("span.spanVaildate").html("格式必须为邮箱类型， 如: example@email.com");
				return false;
			}
		}else{
			$(this).css("box-shadow","none");
		}
	});
	
	$("#accountName").focus(function(){
		$(this).css("box-shadow","0 0 6px #ff0000");
	});
	
	//显示密码点击事件
	$("#input-password-create").click(function(){
		if($(this).attr("checked")){
			$("#password-span").html('<input class="input-text" tabindex="2" id="password" name="password" type="text" value="'+$("#password").val()+'" verify="unspecial/100/1/0"/>');
			$("#ensurePassword-span").html('<input class="input-text" tabindex="3" id="ensurePassword" name="ensurePassword" type="text" value="'+$("#ensurePassword").val()+'" verify="unspecial/100/1/0"/>');
		}else{
			$("#password-span").html('<input class="input-text" tabindex="2" id="password" name="password" type="password" value="'+$("#password").val()+'" verify="unspecial/100/1/0"/>');
			$("#ensurePassword-span").html('<input class="input-text" tabindex="3" id="ensurePassword" name="ensurePassword" type="password" value="'+$("#ensurePassword").val()+'" verify="unspecial/100/1/0"/>');
		}
		//改变密码框样式时触发
		$("#ensurePassword").blur(function(){
			validPassword();
		});
	});
	
	//修改密码点击事件
	$("#input-password-edit").click(function(){
		if($(this).attr("checked")){
			$("#password").attr("disabled",false);
			$("#ensurePassword").attr("disabled",false);
			$("#password").attr("verify","unspecial/100/1/0");
			$("#ensurePassword").attr("verify","unspecial/100/1/0");
		}else{
			$("#password").val("");
			$("#ensurePassword").val("");
			$("#password").attr("disabled",true);
			$("#ensurePassword").attr("disabled",true);
			$("#password").attr("verify","unspecial/100/0/0");
			$("#ensurePassword").attr("verify","unspecial/100/0/0");
			$("#password").css("box-shadow","none").attr('title','');
			$("#password").parent("span").parent("div").find("span.spanVaildate").html("");
			$("#ensurePassword").css("box-shadow","none").attr('title','');
			$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("");
		}
	});
		
	//确认密码与密码一致
	$("#ensurePassword").blur(function(){
		validPassword();
	});
	
	$("#ensurePassword, #password").focus(function(){
		$(this).css("box-shadow","0 0 6px #ff0000");
	});
	
	$("#ensurePassword, #password").blur(function(){
		$(this).css("box-shadow","none");
	});
		
	function validPassword(){
		var password = $("#password").val();
		var ensurePassword = $("#ensurePassword").val();
		if(ensurePassword != ""){
			if(ensurePassword != password){
				$("#ensurePassword").css("box-shadow","0 0 6px #ff0000").attr('title','确认密码必须与密码一致！');
				$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("确认密码必须与密码一致！");
				return false;
			}else{
				$("#ensurePassword").css("box-shadow","none").attr('title','');
				$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("");
			}
		}
	}
	
	//权限控制
	if($(".authority-edit").html()){
		var authority = $("#authority").val();
		var authArr = authority.split(",");
		var checkboxs = $("#checkboxs input[type='checkbox']");
		checkboxs.each(function(i){
			for(var i = 0;i<authArr.length;i++){
				if($(this).val() == authArr[i]){
					$(this).attr("checked","true");
				}
			}
		});
	}
});
		
function exportExcel(){
	var searchValue = $("#searchValue").val();
	var param = "";
	if(searchValue == "请输入关键字"){
		param = "";
	}else{
		param = "?searchValue="+searchValue;
	}
	$("#exportExcel").attr("href",$("#ctx").val()+"/advertiser/main/authority/exportDataReportExcel"+param);
	return false;
}

function isExistName(){
	var flag = false;
	var accountName = $("#accountName").val();
	$.ajax({
       	type: "POST",
        url: $("#ctx").val()+"/advertiser/main/authority/isExist",
        async: false,
        data: {accountName:accountName},
         success: function(data){
	 		if(data == "YES"){
	 			$("#accountName").css("box-shadow","0 0 6px #ff0000").attr('title','你输入的账号已存在！');
	 			$("#accountName").parent("div").find("span.spanVaildate").html("你输入的账号已存在！");
	 			flag = true;
	 		}else{
	 			$("#accountName").css("box-shadow","none").attr('title','');
	 			$("#accountName").parent("div").find("span.spanVaildate").html("");
	 			flag = false;
	 		}
         }
	});
	return flag;
}
